﻿package com.eshore.player;

import java.nio.IntBuffer;

import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


public class PlayerDrawImageThread extends Thread {

	ImagePlayerView view = null;
	int[] sPixels;
	Rect rect;
	boolean isRunning = false;
	protected int imageTimes = 0;
	protected int getReGetImageTimes = 0;
	final int ReGetImageTimes = 200; // 5-6秒

	public PlayerDrawImageThread(ImagePlayerView view) {
		this.view = view;
		this.imageTimes = 0;
	}

	private Rect getImageRect() {
		int newwidth = 352;
		int newheight = 288;
		newwidth = this.view.getImageWidth(0);
		newheight = this.view.getImageWidth(1);
		Log.d("ImagePlayer", "image:" + newwidth + "x" + newheight);
		int newleft = (this.view.getScreenWidth() - newwidth) / 2;
		int newtop = (this.view.getScreenHeight() - newheight) / 2;
		Log.d("ImagePlayer", "screen:" + this.view.getScreenWidth() + "x"
					+ this.view.getScreenHeight());
		Log.d("ImagePlayer", "location: top=" + newtop + "; left="
					+ newleft);
		return new Rect(newleft, newtop, newwidth + newleft, newheight + newtop);
	}

	Handler handler = new Handler() {

		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 1:
				view.createBitmap();
				break;
			case 2:
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public synchronized void run() {
		// int returnvalue = 0;
		isRunning = true;
		Log.d("ImagePlayer", "ffplayGetImage");
		int oldScreenWidth = this.view.getScreenWidth();
		int oldScreenHeight = this.view.getScreenHeight();
		rect = getImageRect();
		// for test
		// sPixels = new int[this.view.getcGetImageWidth()
		// * this.view.getcGetImageHeight()];
		Message message = new Message();
		message.what = 1;
		handler.sendMessage(message);
		this.imageTimes = 0;
		this.getReGetImageTimes = 0;

		try {
			sleep(100);
		} catch (InterruptedException ee) {
			this.pause();
			return;
		}

		while (isRunning) {
			try {
				sPixels = ImagePlayerView.ffplayGetImage();
			} catch (Exception e) {
				Log.d("ImagePlayer", "image Isnot Ready");
				try {
					sleep(30);
				} catch (InterruptedException ee) {
					Log.d("ImagePlayer",
								"ffplay Draw Image canceled with no ready.");
					this.pause();
					return;
				}
				continue;
			}
			if (sPixels == null) {
				Log.d("ImagePlayer", "ffplay Image Length <= 0");
				if (this.getReGetImageTimes > this.ReGetImageTimes) {
					this.pause();
					return;
				}
				this.getReGetImageTimes++;
				try {
					sleep(30);
				} catch (InterruptedException e) {
					Log.d("ImagePlayer",
								"ffplay Draw Image canceled with on image.");
					this.pause();
					return;
				}
				continue;
			}
			this.getReGetImageTimes = 0;
			try {
				sleep(10);
			} catch (InterruptedException e) {
				Log.d("ImagePlayer", "ffplay Draw Image canceled.");
				this.pause();
				return;
			}
			// for test
			// for (int i = 0; i < this.view.getcGetImageWidth()
			// * this.view.getcGetImageHeight(); i++) {
			// sPixels[i] = sPixels[i] + 2;//(byte) 0xFFFFF;
			// }
			try {
				IntBuffer sScreenBuff;
				try {
					if (this.view == null) {
						this.pause();
						return;
					} else {
						sScreenBuff = IntBuffer.allocate(
								this.view.getcGetImageWidth()*
								this.view.getcGetImageHeight());
					}
				} catch (Exception e) {
					Log.d("ImagePlayer",
								"ffplay Draw Image close on CREATE BUFFER.");
					this.pause();
					return;
				}
				sScreenBuff.put(sPixels);
				sScreenBuff.rewind();

				try {
					if (this.view == null) {
						this.pause();
						return;
					} else {
						this.view.getcImage().copyPixelsFromBuffer(sScreenBuff);
					}
				} catch (Exception e) {
					Log.d("ImagePlayer",
								"ffplay Draw Image close on COPY BUFFER.");
					this.pause();
					return;
				}
				try {
					if (this.view == null) {
						this.pause();
						return;
					} else {
						if (oldScreenWidth != this.view.getScreenWidth()
								|| oldScreenHeight != this.view
										.getScreenHeight()) {
							oldScreenWidth = this.view.getScreenWidth();
							oldScreenHeight = this.view.getScreenHeight();
							rect = null;
							rect = getImageRect();
						}
						if (this.imageTimes == 0) {
							//this.view.getPlayer().startTimerThread(); // 显示PTZ等的控制和取消等待框
							this.imageTimes++;
						}
						this.view.getCanvasview().drawBitmap(
								this.view.getcImage(), rect);
					}
				} catch (Exception e) {
					Log.d("ImagePlayer",
								"ffplay Draw Image close on DRAW IMAGE.11111"+(e.getMessage()==null?"":e.getMessage()));
					this.pause();
					return;
				}
				// 每隔多长时间取,作用不大.
				try {
					// IshowEvList.d("ImagePlayer", "fps:" +
					// this.view.getcGetImageFps());
					sleep(1000 / this.view.getcGetImageFps());
				} catch (InterruptedException e) {
					Log.d("ImagePlayer",
								"ffplay Draw Image canceled on waitting.");
				}
			} catch (Exception e) {
				this.pause();
				return;
			}
		}
		Log.d("ImagePlayer", "PlayerDrawImageThread Quit!");
		// try {
		// returnvalue = ImagePlayerView.ffplayStop();
		// } catch (Exception e) {
		// returnvalue = 1;
		// }
		// if (returnvalue != 0) {
		// IshowEvList.d("ImagePlayer", "ffplay cannot stop");
		// // return;
		// }

		// 清屏
		// for (int i = 0; i < this.view.getcGetImageWidth()
		// * this.view.getcGetImageHeight(); i++) {
		// sPixels[i] = (byte) 0x0;
		// }
		// IntBuffer sScreenBuff = IntBuffer.allocate(this.view
		// .getcGetImageWidth()
		// * this.view.getcGetImageHeight());
		// sScreenBuff.put(sPixels);
		// sScreenBuff.rewind();
		// this.view.getcImage().copyPixelsFromBuffer(sScreenBuff);
		// this.view.getCanvasview().drawBitmap(this.view.getcImage(), rect);
	}

	public void pause() {
		this.isRunning = false;
		this.view = null;
		try {
			ImagePlayerView.ffplayStop();
		} catch (Exception e) {

		}
		// try {
		// Thread.sleep(10);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		// int returnvalue = 0;
		// try {
		// returnvalue = ImagePlayerView.ffplayStop();
		// } catch (Exception e) {
		// returnvalue = 1;
		// }
		// if (returnvalue != 0) {
		// IshowEvList.d("ImagePlayer", "ffplay cannot stop");
		// // return;
		// }
	}

	@Override
	public synchronized void start() {
		super.start();
	}

}